package org.server.manage;
import java.io.*;
import java.net.*;

import org.pojo.MessageType;
import org.pojo.User;
import org.pojo.Message.Message;
import org.server.dao.UserDao;
import org.server.tools.ServerTool;
public class ManagerServer {
	/**
	 * 这是qq服务器，它在监听，等待某个qq客户端，来连接
	 */
		public ManagerServer()
		{
			
			try {
				
				//在10000监听
				System.out.println("服务器初始化成功，正在监听端口：10000");
				ServerSocket ss=new ServerSocket(10000);
				//阻塞,等待连接
				while(true)
				{
					Socket s=ss.accept();
					
					//接收客户端发来的信息.
					
					ObjectInputStream ois=new ObjectInputStream(s.getInputStream());	
					ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());	
					Message ms=(Message)ois.readObject();
					if(ms.getMesType().equals(MessageType.message_login)){
						Message m=new Message();
											
						User u=(User)ms.getCon();
						System.out.println("用户:"+u.getUsername()+" 密码:"+u.getPassword());

						UserDao user = new UserDao();
						String passwd = user.getPasswordByUsername(u.getUsername());
						User us = user.loadUserByUsername(u.getUsername());
						if((passwd!=null) && u.getPassword().equals(passwd)&& 
								ServerTool.hm.get(us.getUserid())==null) {
							
							//返回一个成功登陆的信息报		
							m.setMesType(MessageType.message_succeed);
							
							m.setCon(us);//将请求道的用户数据发挥客户端
							oos.writeObject(m);
							System.out.println("用户:"+u.getUsername()+" 登陆成功！");

							//这里就单开一个线程，让该线程与该客户端保持通讯.
							Server2ClientThread scct=new Server2ClientThread(s,us,oos,ois);
							ois=null;oos=null;
							
							//加入用户在线状态
							ServerTool.addClientThread(us.getUserid(), scct);
							ServerTool.addOnlineUser(us);
							//启动与该客户端通信的线程.
							//并通知其它在线用户.
							scct.notifyOther();
							scct.start();				
						}else{
							m.setMesType(MessageType.message_fail);
							oos.writeObject(m);
							//关闭Socket
							s.close();
							
						}
					}else if(ms.getMesType().equals(MessageType.message_register)){
						
						Message m=new Message();
						//ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());		
						User u=(User)ms.getCon();
						
						UserDao user = new UserDao();
						boolean b = user.isExistUsername(u.getUsername());
						if (b || u.getPassword()==null || u.getUsername()==null) {
							m.setMesType(MessageType.message_fail);
							oos.writeObject(m);
							oos.close();
							ois.close();
							s.close();
							System.out.println("注册失败,用户已被注册");
						} else {							
							user.saveUser(u);
							m.setMesType(MessageType.message_succeed);
							oos.writeObject(m);
							System.out.println("注册成功");
							System.out.println("服务器接收到用户id:"+u.getUsername()+"  密码:"+u.getPassword());
							oos.close();
							ois.close();
							s.close();
						}
					}else if(ms.getMesType().equals(MessageType.message_update_password)){
						Message m= new Message();
						User u=(User)ms.getCon();					
						UserDao user = new UserDao();
						String passwd = user.getPasswordByUsername(u.getUsername());
						if(u.getPassword().equals(passwd)) {
							u.setPassword(ms.getChangePasswd());
							user.updatePassword(u);	
							m.setMesType(MessageType.message_succeed);	
							oos.writeObject(m);
							oos.close();
							ois.close();
							s.close();
						}else{
							m.setMesType(MessageType.message_fail);	
							oos.writeObject(m);
							oos.close();
							ois.close();
							s.close();
						}
					}
				}					
			} catch (Exception e) {
				e.printStackTrace();
				// TODO: handle exception
			}
			
		}
		public static void main(String[] args)
		{
			ManagerServer ms  = new ManagerServer();
		}
		
	}
